<?php

/**
 * socepmi actions.
 *
 * @package    sispatr
 * @subpackage socepmi
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class tiposActions extends sfActionsFoxsis
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('tipos', 'list');
  }
  
    public function executeList()
	{
	
		$this->colunas	= array("id" 			=> "id_tipo",
								"Nome"			=> "nm_tipo", 
								"Ações"			=> 'act');
		
        $this->setTemplate('index');
    }
    
    public function executeGrid()
    {
    	try
    	{
    		$output	= TipoTable::carregar();
    		return $this->setJSONJqGrid($output, "id_tipo");
    	}
    	catch(Exception $e)
    	{
    		$output	= array('erro' => $e->getMessage());
    		$this->getResponse()->setHttpHeader('Content-type','application/json; charset=utf-8');
	      	return $this->renderText(json_encode($output));
    	}
    }
    
    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		); 

		$this->getUser()->setAttribute(
			sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);
		
		return $this->redirect($this->getModuleName().'/list');
	}

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new SistemaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new SistemaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($sistema = Doctrine::getTable('Sistema')->find(array($request->getParameter('id_sistema'))), sprintf('Object sistema does not exist (%s).', $request->getParameter('id_sistema')));
    $this->form = new SistemaForm($sistema);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($sistema = Doctrine::getTable('Sistema')->find(array($request->getParameter('id_sistema'))), sprintf('Object sistema does not exist (%s).', $request->getParameter('id_sistema')));
    $this->form = new SistemaForm($sistema);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($sistema = Doctrine::getTable('Sistema')->find(array($request->getParameter('id_sistema'))), sprintf('Object sistema does not exist (%s).', $request->getParameter('id_sistema')));
    $sistema->delete();

    $this->redirect('socepmi/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('socepmi/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sistema = $form->save();
	  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
      $this->redirect('socepmi/edit?id_sistema='.$sistema->getIdSistema());
    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
